package com.yhp.web;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(urlPatterns = {"/downing"})
public class DownServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受下载的文件名
        String filename=req.getParameter("filename");
        String url="/myuploadfile/"+filename;
        //2.将响应的内容设置为通用的二进制流
        resp.setContentType("application/octet-stream");
        //attachment 告诉浏览器以附件的方式下载文件(弹出下载框)
        filename= URLEncoder.encode(filename,"utf-8");
        resp.addHeader("Content-Disposition","attachment;filename="+filename);
        req.getRequestDispatcher(url).forward(req, resp);
        //清空缓冲区:将服务端缓冲区的
        resp.flushBuffer();
    }
}
